//
//  ModiSelfInformationVC.m
//  Yihubang
//
//  Created by hyu on 2016/12/1.
//  Copyright © 2016年 xmy. All rights reserved.
//

#import "ModiSelfInformationVC.h"
#import "ModiSelfModel.h"
#import "PickDateView.h"
@interface ModiSelfInformationVC ()<PickDateViewDelegate>
@property (nonatomic , strong) ModiSelfModel * viewModel;
@property (nonatomic , strong) PickDateView *pickDateView;
@end

@implementation ModiSelfInformationVC
@synthesize viewModel = _viewModel;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:NO];
    self.viewModel.title =@"个人信息";
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification"
                                              object:self.nickName];
    UIBarButtonItem *saveItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(accomplishAction:)];
    [self.navigationItem setRightBarButtonItem:saveItem];
    self.viewModel.updateInfo = [NSMutableDictionary dictionaryWithDictionary:self.userClient.userInfo];
    self.characteristic.placeholderStr =@"譬如：胎记，疤痕等其他体貌特征。信息的完善对你非常有帮助！";
    self.characteristic.placeholderColor = [UIColor colorWithHex:0xA8A8A8];
    [self settingUserInfo];
    // Do any additional setup after loading the view.
}

- (void)gotoBack {
    [self.navigationController.navigationBar setHidden:YES];
    [self.navigationController popViewControllerAnimated:YES];
    
}
-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.characteristicViewHeightConstraint.constant = RatioBase6(150);
}
- (void)bindViewModel
{
    [super bindViewModel];
    @weakify(self)
    //绑定数据
    [self.nickName.rac_textSignal subscribeNext:^(NSString*nickname) {
        @strongify(self);
        [self.viewModel.updateInfo setObject:nickname forKey:@"nickname"];
    }] ;
    [self.height.rac_textSignal subscribeNext:^(NSString*height) {
        @strongify(self);
        [self.viewModel.updateInfo setObject:height forKey:@"stature"];
    }] ;
    [self.weight.rac_textSignal subscribeNext:^(NSString*weight) {
        @strongify(self);
        [self.viewModel.updateInfo setObject:weight forKey:@"shape"];
    }] ;
    [self.nickName.rac_textSignal subscribeNext:^(NSString*characteristic) {
        @strongify(self);
        [self.viewModel.updateInfo setObject:characteristic forKey:@"characteristic"];
    }] ;
    [self.characteristic.rac_textSignal subscribeNext:^(NSString *characteristic) {
        [self.viewModel.updateInfo setObject:characteristic forKey:@"anaphylaxis"];
    }];
    [RACObserve(self.female,enabled) subscribeNext:^(NSNumber*signupActive){
        @strongify(self)
        if(![signupActive boolValue]){
            [self.viewModel.updateInfo setObject:@"2" forKey:@"sex"];
            [self.female setBackgroundColor:[UIColor colorWithHex:0xB4E4E2]];
        }else{
            [self.female setBackgroundColor:[UIColor colorWithHex:0xD8D8D8 ]];
        }
    }];
    [RACObserve(self.male,enabled) subscribeNext:^(NSNumber*signupActive){
        @strongify(self)
        if(![signupActive boolValue]){
            [self.viewModel.updateInfo setObject:@"1" forKey:@"sex"];
             [self.male setBackgroundColor:[UIColor colorWithHex:0xB4E4E2]];
        }else{
             [self.male setBackgroundColor:[UIColor colorWithHex:0xD8D8D8 ]];
        }
    }];
    [[self.female rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(id x) {
         @strongify(self)
         self.female.enabled = NO;
         self.male.enabled = YES;
         self.viewModel.sex = NO;
     }];
    [[self.male rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(id x) {
        @strongify(self)
        self.male.enabled = NO;
        self.female.enabled = YES;
        self.viewModel.sex = YES;
        
    }];
    [self.headImage setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self)
        [self.viewModel.updateAgainCommand execute:self.headImage];
    }];
    [self.pickDate setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self)
        [self.view addSubview:self.pickDateView];
        [UIView animateWithDuration:0.4 animations:^{
            _pickDateView.frame = CGRectMake(0, ScreenHeight-287, ScreenWidth, 223);
        }];
    }];
}

-(PickDateView *)pickDateView{
    if (!_pickDateView) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PickDateView" owner:nil options:nil];
        _pickDateView = [nib firstObject];
        _pickDateView.delegate = self;
        _pickDateView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 223);
    }
    return _pickDateView;
}

-(void)ChangeDate:(NSString *)dateStr{
    if(dateStr){
        [self.viewModel.updateInfo setObject:dateStr forKey:@"age"];
        self.birthdayLabel.text = dateStr;
    }
    self.pickDateView = nil;
}

- (void)accomplishAction:(id)sender {
    [self.viewModel.saveInfoCommend execute:self.headImage.image];
}

- (void)settingUserInfo{
    [self.headImage setImageWithURLString:self.userClient.userInfo[@"img_top"] placeholderImage:nil];
    self.nickName.text = self.userClient.userInfo[@"nikename"];
    self.birthdayLabel.text = [self.userClient.userInfo[@"age"] length]>0 ? self.userClient.userInfo[@"age"]:@"1990-01-23";
    self.height.text = self.userClient.userInfo[@"stature"];
    self.weight.text = self.userClient.userInfo[@"shape"];
    if ([self.userClient.userInfo[@"sex"] intValue] == 1) {
        self.male.enabled = NO;
    }else{
        self.female.enabled = NO;
    }
    NSLog(@"%@",self.userClient.userInfo[@"anaphylaxis"]);
    self.characteristic.text = self.userClient.userInfo[@"anaphylaxis"];
}

- (ModiSelfModel*)viewModel{
    if (!_viewModel) {
        _viewModel = [[ModiSelfModel alloc] initWithViewController:self];
    }
    return _viewModel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
